package com.baichuan.zntx.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baichuan.common.core.utils.MapstructUtils;
import com.baichuan.common.core.utils.StringUtils;
import com.baichuan.common.mybatis.core.page.TableDataInfo;
import com.baichuan.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baichuan.zntx.domain.bo.TaskBo;
import com.baichuan.zntx.domain.vo.TaskVo;
import com.baichuan.zntx.domain.Task;
import com.baichuan.zntx.mapper.TaskMapper;
import com.baichuan.zntx.service.ITaskService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 任务记录Service业务层处理
 *
 * @author MaZhanliang
 * @date 2024-05-22
 */
@RequiredArgsConstructor
@Service
public class TaskServiceImpl implements ITaskService {

    private final TaskMapper baseMapper;

    /**
     * 查询任务记录
     */
    @Override
    public TaskVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询任务记录列表
     */
    @Override
    public TableDataInfo<TaskVo> queryPageList(TaskBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Task> lqw = buildQueryWrapper(bo);
        Page<TaskVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询任务记录列表
     */
    @Override
    public List<TaskVo> queryList(TaskBo bo) {
        LambdaQueryWrapper<Task> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Task> buildQueryWrapper(TaskBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Task> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getDeviceId()), Task::getDeviceId, bo.getDeviceId());
        lqw.eq(bo.getTaskType() != null, Task::getTaskType, bo.getTaskType());
        lqw.eq(bo.getStatus() != null, Task::getStatus, bo.getStatus());
        lqw.between(params.get("beginExcuteTime") != null && params.get("endExcuteTime") != null,
            Task::getExcuteTime ,params.get("beginExcuteTime"), params.get("endExcuteTime"));
        lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
            Task::getCreateTime ,params.get("beginCreateTime"), params.get("endCreateTime"));
        return lqw;
    }

    /**
     * 新增任务记录
     */
    @Override
    public Boolean insertByBo(TaskBo bo) {
        //判断是否存在未执行的任务
        Task add = baseMapper.selectOne(new LambdaQueryWrapper<Task>().eq(Task::getDeviceId, bo.getDeviceId()).in(Task::getStatus, 0,1));
        if(ObjectUtil.isNotNull(add)){
           return false;
        }
        add = MapstructUtils.convert(bo, Task.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改任务记录
     */
    @Override
    public Boolean updateByBo(TaskBo bo) {
        Task update = MapstructUtils.convert(bo, Task.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Task entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除任务记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Task queryByDevice(String id) {
        return baseMapper.selectOne(new LambdaQueryWrapper<Task>().eq(Task::getDeviceId, id).orderByDesc(Task::getCreateTime).last("limit 1"));
    }

    @Override
    public boolean updateById(Task task) {
        return baseMapper.updateById(task) > 0;
    }

    @Override
    public void queryTimeoutTask() {

    }
}
